﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Marbles.Core.Content.Links;
using Marbles.Core.Content.Sites;

namespace Marbles.Core.Handlers
{
    public class LinkHandler : IHttpHandlerBase
    {

        public void ProcessRequest(HttpContext context)
        {
            string id = string.Empty;
            if (!string.IsNullOrEmpty(context.Request.Params["id"])) { id = context.Request.Params["id"]; }

            if (string.IsNullOrEmpty(id))
            {
                context.Response.StatusCode = 404;
                context.Response.Status = "Link not found.";
                context.Response.StatusDescription = "Link not found.";
                context.Response.End();
                return;
            }

            Link link = new Link(SiteDefinition.GetCurrent().SiteId, id);
            link.UpdateLastAccess();

            if (link != null)
            {
                context.Response.Redirect(link.Url, true);
            }
        }

        public string Verbs
        {
            get { return "*"; }
        }

        public bool Validate
        {
            get { return false; }
        }

        public string Path
        {
            get { return "Link.axd"; }
        }

        public string Name
        {
            get { return this.GetType().Name; }
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}